<!--组件-其他设置-->
<template>

  <div class="bg" v-if="isOk">

    <div class="row acenter" @click="showClick(!isShow)">
      <text class="font28 main-t1">{{T('其他设置')}}</text>
      <LImage class="arrow"  :src="isShow ? Config.img('xiala_blue_.png') : Config.img('xiala_blue.png')"></LImage>
    </div>

    <div v-if="isShow">
      <div class="bg">
        <!--总收益止盈-->
        <LInput class="input" v-model="stopProfit_c" :comment="stopProfitTipe" :title="T('总收益止盈')" :tips="T('选填')" :unit="'%'"></LInput>
        
        <!--总亏损止盈-->
        <LInput class="input" v-model="stopLoss_c" :comment="stopLossTipe" :title="T('总亏损止损')" :tips="T('选填')" :unit="'%'"></LInput>

        <!--定时自动暂停-->
        <!-- <LInput class="input" 
        v-model="pauseShowTime" 
        :comment="pauseTimeTipe" 
        :title="T('定时自动暂停')"  
        :tips="T('选填')" 
        :unit="(pauseTimeSelect ? T('分') : null)" 
        @input="pauseOnInput" @focus="pauseOnFocus" @blur="pauseOnBlur"></LInput> -->

        <!--定时自动终止-->
        <LInput class="input" 
        v-model="showTime" 
        :comment="timeStopStampTipe" 
        :title="T('定时自动终止')"  
        :tips="T('选填')" 
        :unit="(timeSelect ? T('分') : null)" 
        @input="onInput" @focus="onFocus" @blur="onBlur"></LInput>

        <div v-if="!isZi">
          <!--撤销订单-->
          <div class="height40"></div>
          <LSwitc class="switc" v-model="cancelOrder_c" :comment="cancelOrderTipe" :title="T('终止后撤销未成交的订单')"></LSwitc>

          <!--兑换币种-->
          <div class="height40"></div>
          <LSwitc class="switc" v-model="isConvert_c" :comment="isConvertTipe" :title="T('终止后自动兑换成投入币种')"></LSwitc>
        </div>
      </div>
      <div class="mantle" v-if="isEdit" @click="click"></div>
    </div>
  </div>
</template>

<script>

//组件
import LInput from "@/public/view/LInput.vue";
import LSwitc from "@/public/view/LSwitc.vue";

//功能
const globalEvent = weex.requireModule('globalEvent');
import math from "@/_js/math";
import modal from "@/_js/modal";
import Config from "@/_js/config";

import I18N from "@/_i18n/i18n";
var T = I18N.t;

export default {

  components: {
    LInput,
    LSwitc,
  },

  model:{
    prop:"isShow",
    event:"change",
  },
  
  props:{

    isShow:{ //turn显示
      type:Boolean,
      default:false,
    },

    isEdit:{ //turn不可编辑
      type:Boolean,
      default:false,
    },

    isZi:{ //turn子币对
      type:Boolean,
      default:false,
    },

    stopProfit:String, //止盈
    stopLoss:String, //止损
    pauseTimeStamp:String, //暂停时间戳
    timeStopStamp:String, //终止时间戳
    cancelOrder:{ //true 撤销订单
      type:Boolean,
      default:true,
    },
    isConvert:{ //true 兑换币种
      type:Boolean,
      default:true,
    },
  },
  
  data() {
    return {
      T:T,
      Config:Config,
      isOk:false, //turn绘制

      timeStamp:'', //当前时间戳

      pauseTimeSelect:false, //true 时间输入框选中
      pauseShowTime:'', //显示的时间
      pauseTimeMinute:'', //暂停时间-分
      pauseTime:'', //暂停时间
      pauseTimeStamp_c:this.pauseTimeStamp?this.pauseTimeStamp:'', //暂停时间戳
      pauseTimeTipe:T('策略暂停操作优先级高于止盈止损及定时终止，即暂停状态下此类操作不生效。定时终止可能会有几分钟的偏差，触发后自动终止策略，系统根据你的选择决定是否执行撤单兑换操作。'),

      timeSelect:false, //turn 选中输入框
      showTime:'', //显示的时间
      timeMinute:'', //终止时间-分
      timeStop:'', //终止时间
      timeStopStamp_c:this.timeStopStamp?this.timeStopStamp:'', //终止时间戳
      timeStopStampTipe:T('策略暂停操作优先级高于止盈止损及定时终止，即暂停状态下此类操作不生效。定时终止可能会有几分钟的偏差，触发后自动终止策略，系统根据你的选择决定是否执行撤单兑换操作。'),

      stopProfit_c:this.stopProfit?this.stopProfit:'', //止盈
      stopLoss_c:this.stopLoss?this.stopLoss:'', //止损
      
      cancelOrder_c:this.cancelOrder, //true 撤销订单
      isConvert_c:this.isConvert, //true 兑换币种
      
      stopProfitTipe:T('止盈根据总收益率计算，策略暂停操作优先级高于止盈止损及定时终止，即暂停状态下此类操作不生效。触发止盈后自动终止策略，系统根据你的选择决定是否执行撤单兑换操作。'),
      stopLossTipe:T('止损根据总收益率计算，策略暂停操作优先级高于止盈止损及定时终止，即暂停状态下此类操作不生效。触发止损后自动终止策略，系统根据你的选择决定是否执行撤单兑换操作。'),
      cancelOrderTipe:T('开启此选项，策略终止时将自动撤销所有未成交的委托单。'),
      isConvertTipe:T('开启此选项，策略终止时将自动将资产以市价进行委托兑换成投入币种；如果未开启终止后撤销未成交委托单功能，将只兑换未冻结部分的资产。深度不足的币种可能面临风险，具体兑换结果以交易所为准。'),
    };
  },

  mounted(){
    this.initData();
    if (this.isEdit) {
      this.stopProfitTipe = undefined;
      this.stopLossTipe= undefined;
      this.pauseTimeTipe = undefined;
      this.timeStopStampTipe= undefined;
      this.cancelOrderTipe= undefined;
      this.isConvertTipe= undefined;
    }
    this.isOk = true;
  },

  methods:{

    //初始化数据
    initData(){
  
      //还原输入-时间分
      this.timeMinute = '';
      this.pauseTimeMinute = '';

      //获取当前时间，去除秒
      var timeStamp = (new Date().getTime()) + '';
      timeStamp = math.chu(timeStamp, '60000', 0, 2);
      this.timeStamp = math.cheng(timeStamp, '60000', 0, 2);

      //运算终止时间分
      if (this.pauseTimeStamp_c.length) {

        var time = math.jian(this.pauseTimeStamp_c, this.timeStamp);
        var pauseTimeMinute = math.chu(time, '60000', 0, 2);
        if (math.getNumber(pauseTimeMinute) < 0) {
          this.timeStamp = this.pauseTimeStamp_c;
          this.pauseTimeMinute = '0';
        } else {
          this.pauseTimeMinute = pauseTimeMinute;
        }
      }
      if (this.timeStopStamp_c.length) {

        var time = math.jian(this.timeStopStamp_c, this.timeStamp);
        var timeMinute = math.chu(time, '60000', 0, 2);
        if (math.getNumber(timeMinute) < 0) {
          this.timeStamp = this.timeStopStamp_c;
          this.timeMinute = '0';
        } else {
          this.timeMinute = timeMinute;
        }
      }

      //4、设置显示时间-终止时间
      this.pauseShowTime = this.getTimeStop(this.pauseTimeMinute);
      this.showTime = this.getTimeStop(this.timeMinute);
    },

    //刷新视图
    refresh(){

      this.$emit("change", false);

      this.stopProfit_c = ''; //止盈
      this.stopLoss_c = ''; //止损
      this.pauseTimeStamp_c = ''; //暂停时间戳
      this.timeStopStamp_c = ''; //终止时间戳

      this.cancelOrder_c = true; //true 撤销订单
      this.isConvert_c = true; //true 兑换币种

      this.initData();
    },

    //检查
    checkError(){

      if (this.stopProfit_c.length > 0 && math.getNumber(this.stopProfit_c) == 0) {
        modal.toast(T("总收益止盈不可为0"));
        return true;
      }
      if (this.stopLoss_c.length > 0 && math.getNumber(this.stopLoss_c) == 0) {
        modal.toast(T("总亏损止损不可为0"));
        return true;
      }

      var stamp1 = this.getTimeStopStamp(this.pauseTimeMinute);
      stamp1 = math.getNumber(stamp1);

      var stamp2 = (new Date().getTime())+ '';
      stamp2 = math.getNumber(stamp2);

      if (this.pauseTimeMinute.length > 0 && stamp2 >= stamp1) {
        modal.toast(T("定时自动暂停时间已过期"));
        return true;
      }
      
      var stamp1 = this.getTimeStopStamp(this.timeMinute);
      stamp1 = math.getNumber(stamp1);

      var stamp2 = (new Date().getTime())+ '';
      stamp2 = math.getNumber(stamp2);

      if (this.timeMinute.length > 0 && stamp2 >= stamp1) {
        modal.toast(T("定时自动终止时间已过期"));
        return true;
      }

      return false;
    },
    
    //获取参数
    getParam(){

      var param = {'cancelOrder':this.cancelOrder_c, //撤销订单
                    'isConvert':this.isConvert_c, //兑换币种
                  };

        param['stopProfit'] = this.stopProfit_c.length ? math.chu(this.stopProfit_c, '100') : '';
        param['stopLoss'] = this.stopLoss_c.length ? math.chu(this.stopLoss_c, '-100') : '';
        param['pauseTime'] = this.getTimeStopStamp(this.pauseTimeMinute);
        param['stopTime'] = this.getTimeStopStamp(this.timeMinute);
        return param;
    },
    
    /********************************** Get方法*/
    //获取终止时间戳
    getTimeStopStamp(timeMinute) {
      if (timeMinute.length == 0) {
        return '';
      }

      var time = math.cheng(timeMinute, '60000');
      time = math.jia(this.timeStamp, time);
      return time;
    },

    //获取终止时间-年/月/日 时:分
    getTimeStop(timeMinute) {

      if (timeMinute.length == 0) {
        return '';
      }

      var time = this.getTimeStopStamp(timeMinute);
      time = math.getNumber(time);
      time = this.getTime(time);  
      return time;
    },

    //获取时间-年/月/日 时:分
    getTime(timestamp){

      var time = new Date(Number(timestamp));

      var year = time.getFullYear()

      var month = time.getMonth()+1
      month = month < 10 ? "0"+month : month;

      var day = time.getDate();
      day = day < 10 ? "0"+day : day;

      var hour = time.getHours();
      hour = hour < 10 ? "0"+hour : hour;

      var minute = time.getMinutes();
      minute = minute < 10 ? "0"+minute : minute;

      return year+"/"+month+"/"+day+' '+hour+':'+minute;
    },

    /**********************************************输入框回调*/
    //输入框-选中
    pauseOnFocus(e) {
      this.pauseTimeSelect = true;
      this.pauseShowTime = this.pauseTimeMinute;
    },

    //输入框-取消选中
    pauseOnBlur(e) {
      this.pauseTimeSelect = false;
      this.pauseShowTime = this.getTimeStop(this.pauseTimeMinute);
    },

    ///输入内容改变
    pauseOnInput(e){
      var value = math.getNumber(e.value) > 999999 ? '999999' : e.value;
      this.pauseTimeMinute = value;
    },

    //输入框-选中
    onFocus(e) {
      this.timeSelect = true;
      this.showTime = this.timeMinute;
    },

    //输入框-取消选中
    onBlur(e) {
      this.timeSelect = false;
      this.showTime = this.getTimeStop();
    },

    ///输入内容改变
    onInput(e){
      var value = math.getNumber(e.value) > 999999 ? '999999' : e.value;
      this.timeMinute = value;
    },

    
    /********************************** 点击触发*/
    //显隐点击
    showClick(isShow){
      this.$emit("change", isShow);
    },

    //遮挡层，点击遮挡
    click(){

    },
  }
}
</script>


<style src="@/_css/style.css"></style>
<style scoped>
.bg{
  width: 670px;
}
.mantle{
  width: 670px;
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}

.arrow{
  width: 20px;
  height: 20px;
  margin-left: 20px;
  margin-right: 20px;
}

.input {
  margin-top: 40px;
  width: 670px;
}

.switc{
  width: 670px;
}

/*************** 高度*/
.height40{
  height: 40px;
}
</style>
